package com.ecoolex.discount.sales.core.manager;

import com.ecoolex.discount.sales.core.dao.CardVoucherPackageDAO;
import com.ecoolex.discount.sales.entity.CardVoucherPackage;
import com.ecoolex.discount.sales.entity.CardVoucherPackage.Column;
import com.ecoolex.discount.sales.entity.CardVoucherPackageExample;
import com.ecoolex.discount.sales.entity.CardVoucherPackageExample.Criteria;
import com.ecoolex.discount.sales.entity.DiscountOrder;
import com.ecoolex.discount.sales.entity.DiscountOrderDetail;
import com.ecoolex.discount.sales.enums.CardVoucherPackageStatus;
import com.ecoolex.discount.sales.enums.DiscountProductDiscountMode;
import com.ecoolex.framework.common.pojo.Page;
import com.ecoolex.framework.common.pojo.Pagination;
import com.ecoolex.framework.common.util.Check;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.Instant;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import static com.ecoolex.discount.sales.entity.CardVoucherPackage.NOT_DELETED;

@Service
public class CardVoucherPackageManager {
    @Autowired
    private CardVoucherPackageDAO dao;
    
    public CardVoucherPackage getCardVoucherPackage(Integer cardVoucherId, Integer userId) {
    	CardVoucherPackageExample example = new CardVoucherPackageExample();
        Criteria criteria = example.createCriteria();
        criteria.andCardVoucherIdEqualTo(cardVoucherId)
        .andUserIdEqualTo(userId)
        .andStatusNotEqualTo(CardVoucherPackageStatus.EXPIRED.real())
        .andDeletedEqualTo(CardVoucherPackage.NOT_DELETED);
    	return dao.selectOneByExample(example);
	}

    public Pagination<CardVoucherPackage> query(Integer userId,Page page, Integer discountMode) {
        CardVoucherPackageExample example = new CardVoucherPackageExample();
        Criteria criteria = example.createCriteria();
        if(Check.notNull(discountMode)) {
        	criteria.andDiscountModeEqualTo(discountMode);
        }
        criteria.andUserIdEqualTo(userId)
                .andStatusNotEqualTo(CardVoucherPackageStatus.EXPIRED.real())
                .andDeletedEqualTo(NOT_DELETED);
        example.setOffset(page.getFirstIndex());
        example.setRows(page.getPageSize());
        example.orderBy(Column.createTime.desc());

        return Pagination.create(page, 
        	dao.countByExample(example),
        	() -> dao.selectByExample(example));
    }

    void add(DiscountOrder order, List<DiscountOrderDetail> detailList, String createUser) {
        if (DiscountProductDiscountMode.GIFT_CARD.notEqual(order.getDiscountMode())
                && DiscountProductDiscountMode.VOUCHERS.notEqual(order.getDiscountMode())) {
            // 不是卡券, 则不处理
            return;
        }

        List<CardVoucherPackage> packageList = detailList.stream().map(detail -> {
            CardVoucherPackage record = new CardVoucherPackage();
            record.setUserId(order.getUserId());
            record.setOrderId(order.getOrderId());
            record.setProductId(detail.getProductId());
            record.setOrderDetailId(detail.getOrderDetailId());
            record.setDiscountMode(order.getDiscountMode());
            record.setVoucherNumber(detail.getVoucherNumber());
            record.setVoucherPass(detail.getVoucherPass());
            record.setEffectivetime(detail.getEffectivetime());
            record.setExpiretime(detail.getExpiretime());
            record.setStatus(CardVoucherPackageStatus.UNUSED.real());
            record.setDeleted(NOT_DELETED);
            record.setCreateUser(createUser);
            record.setCreateTime(Date.from(Instant.now()));
            record.setUpdateUser(createUser);
            record.setUpdateTime(Date.from(Instant.now()));
            return record;
        }).collect(Collectors.toList());
        // 批量添加到卡包
        dao.batchInsert(packageList);
    }

    /**
     * 更新到使用状态
     * @param orderId
     * @param updateUser
     */
    void updateStatusToUsed(Integer orderId, String updateUser) {
        CardVoucherPackage record = new CardVoucherPackage();
        record.setStatus(CardVoucherPackageStatus.USED.real());
        record.setUpdateUser(updateUser);
        record.setUpdateTime(Date.from(Instant.now()));

        CardVoucherPackageExample example = new CardVoucherPackageExample();
        example.createCriteria()
                .andOrderIdEqualTo(orderId)
                .andStatusEqualTo(CardVoucherPackageStatus.UNUSED.real())
                .andDeletedEqualTo(NOT_DELETED);

        dao.updateByExampleSelective(record,
                example,
                Column.status,
                Column.updateUser,
                Column.updateTime);
    }

    // 标记过期

    // 删除 (退款时)
    /**
     * @param orderId
     * @param updateUser
     */
    public void delete(Integer orderId, String updateUser) {
        CardVoucherPackage record = new CardVoucherPackage();
        record.setDeleted(CardVoucherPackage.IS_DELETED);
        record.setUpdateUser(updateUser);
        record.setUpdateTime(Date.from(Instant.now()));

        CardVoucherPackageExample example = new CardVoucherPackageExample();
        example.createCriteria()
                .andOrderIdEqualTo(orderId)
                .andDeletedEqualTo(NOT_DELETED);

        dao.updateByExampleSelective(record,
                example,
                Column.deleted,
                Column.updateUser,
                Column.updateTime);
    }
}